﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using LowLevelGraphics.Filter;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// 
    /// </summary>
    public class Experimental2 : Experimental1
    {
        protected EXPERIMENTAL1 M_MODE = EXPERIMENTAL1.BRIGHTNESS;
        protected float m_fThreshold = 0.5f;

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental2"/> class.
        /// </summary>
        public Experimental2()
            : this(EXPERIMENTAL1.BRIGHTNESS, 0.5f)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental2"/> class.
        /// </summary>
        /// <param name="_MODE">The _ MODE.</param>
        /// <param name="_fThreshold">The _f threshold.</param>
        public Experimental2(EXPERIMENTAL1 _MODE, float _fThreshold)
        {
            M_EXPERIMENTAL1 = _MODE;
            m_fThreshold = _fThreshold;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Experimental2"/> class.
        /// </summary>
        /// <param name="_experimental2">The _experimental2.</param>
        internal Experimental2(Experimental2 _experimental2)
        {
            this.M_MODE = _experimental2.M_MODE;
            this.m_fThreshold = _experimental2.m_fThreshold;
        }

        /// <summary>
        /// gets or sets experimental mode
        /// </summary>
        public EXPERIMENTAL1 MODE
        {
            get { return M_MODE; }
            set { M_MODE = value; }
        }

        /// <summary>
        /// Gets or sets threshold
        /// </summary>
        public float Threshold
        {
            get { return m_fThreshold; }
            set { m_fThreshold = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            ColorCalculation calculation = m_aDict[M_MODE];
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            UnsafeBitmap bitmap = _bitmap;

            Color color = Color.Transparent;
            Color colorEnvironment = Color.Transparent;
            bitmap.Clear(m_ColorBackground);

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    color = bitmapCloned.GetPixel(x, y);

                    int nX = 0;
                    int nY = 0;
                    float nCompleteEnvironmentSum = 0;
                    foreach (Point ptPoint in Definitions.EnvironmentPointListClockwise)
                    {
                        nX = x + ptPoint.X;
                        nY = y + ptPoint.Y;
                        //bitmap.SetPixel(x, y, Definitions.Black);
                        if (nX < nWidth - 1 && nY < nHeight - 1 && nX > -1 && nY > -1)
                        {
                            colorEnvironment = bitmapCloned.GetPixel(nX, nY);
                            float nChannelSum = calculation(colorEnvironment);
                            nCompleteEnvironmentSum += nChannelSum;
                        }
                    }
                    nCompleteEnvironmentSum /= 8;
                    bitmap.SetPixel(x, y, (nCompleteEnvironmentSum > m_fThreshold ? m_ColorForeground : m_ColorBackground));
                }
            }
            bitmapCloned.Dispose();
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Experimental2(this);
        }
    }
}
